//var apiHost ="http://127.0.0.1:8002";
//var apiHost = "http://www.xiaofang.com";
var apiHost = "https://fireblue.gshyj.com"

var version ="1.0.2.44"
const formatTime = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}

const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : '0' + n
}
function getHeader(){
  try {
    var token = wx.getStorageSync('token')
    if (token) {
      return { "version": version, "token": token};
    }
  } catch (e) {
    // Do something when catch error
    return { "version": version};
  }
  
}
function upload(tempFilePaths,name,that){
  wx.showToast({
    title: '正在上传...',
    icon: 'loading',
    mask: true,
    duration: 100000
  })
  
  var uploadImgCount = 0;
  var urls=[];
  for (var i = 0, h = tempFilePaths.length; i < h; i++) {
    wx.uploadFile({
      url: apiHost + '/weixinMiniUpload',
      filePath: tempFilePaths[i],
      name: 'file',
      header: getHeader(),
      success: function (res) {
        uploadImgCount++;
        var data = JSON.parse(res.data); 
        if (data.code == 100003) {
          wx.showToast({
            title: '登陆过期，重新登陆',
            icon: 'loading',
            mask: true,
            duration: 1000
          });
          wx.clearStorageSync()
          login();
        } else {
          
          urls.push(data.data);
        

          //如果是最后一张,则隐藏等待中  
          if (uploadImgCount == tempFilePaths.length) {
            that.setData({
              [name]: urls
            });
            wx.hideToast();
          }
        }
      },
      fail: function (res) {
        urls=[];
        wx.hideToast();
        wx.showModal({
          title: '错误提示',
          content: '上传图片失败',
          showCancel: false,
          success: function (res) { }
        })
      }
    });
  }   
}
function lotUpload(tempFilePaths, name, that) {
  wx.showToast({
    title: '正在上传...',
    icon: 'loading',
    mask: true,
    duration: 100000
  })

  var uploadImgCount = 0;
  var urls = [];
  for (var i = 0, h = tempFilePaths.length; i < h; i++) {
    wx.uploadFile({
      url: apiHost + '/weixinMiniUpload',
      filePath: tempFilePaths[i],
      name: 'file',
      header: getHeader(),
      success: function (res) {
        uploadImgCount++;
        var data = JSON.parse(res.data);
        if (data.code == 100003) {
          wx.showToast({
            title: '登陆过期，重新登陆',
            icon: 'loading',
            mask: true,
            duration: 1000
          });
          wx.clearStorageSync()
          login();
        } else {

          urls.push(data.data);


          //如果是最后一张,则隐藏等待中  
          if (uploadImgCount == tempFilePaths.length) {
            that.setData({
              [name]: that.data[name].concat(urls)
            });
            wx.hideToast();
          }
        }
      },
      fail: function (res) {
        urls = [];
        wx.hideToast();
        wx.showModal({
          title: '错误提示',
          content: '上传图片失败',
          showCancel: false,
          success: function (res) { }
        })
      }
    });
  }
}

function login(){
  wx.login({
    success: function (res) {
      console.log(res);

      if (res.code) {
        //发起网络请求
        request('/gateway/member/login', { code: res.code },
          function (data) {
            console.log(data);
            if (data.code == 0) {
              wx.setStorageSync("openid", data.data.openId);
            
            } else {
              wx.showToast({
                title: '登陆失败' + data.msg,
                icon: 'none',
                duration: 1000
              })
            }
          
          });



      } else {
        wx.showToast({
          title: '微信授权失败',
          icon: 'none',
          duration: 2000
        })
      }

    }
  })


}
function request(url, data, callback){
  console.log("requestData:");
  console.log(data);
  wx.request({
    url: apiHost + url,
    header: getHeader(),
    method: "POST",
    data: data,
    success: function (res) {
      data = res.data;
     // console.log(data)
      if (data.code==100003){
        wx.showToast({
          title: '登陆过期，重新登陆',
          icon: 'loading',
          mask: true,
          duration: 1000
        })
        wx.clearStorageSync()
        login();
      }else{
        callback(data);
      }
       
    }
  })
  
}
module.exports = {
  formatTime: formatTime,
  request:request,
  upload:upload,
  lotUpload: lotUpload,
  login:login
}
